import { createRouter, createWebHistory } from 'vue-router'

import loginRoutes from './login.routes'
import layoutRoutes from './layout.routes'
import MineRoutes from './mine.routes'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/mainlayout',
    },
    // 登录页面路由
    ...loginRoutes,

    // 默认页面路由(主界面)
    ...layoutRoutes,

    // 我的界面路由
    ...MineRoutes,

    // 404页面
    {
      path: '/:pathMatch(.*)*',
      name: '404',
      component: () => import('@/views/error/ErrorView.vue'),
    },
  ],

  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  },
})

// 路由刷新判断跳转
import { useUserStore } from '@/stores'

// router.beforeEach((to, from, next) => {
//   const isReload = from.name === undefined
//   if (isReload && to.path === '/login/setpassword') {
//     // 可以选择跳转到 signin 或 signup，取决于你的业务
//     return next('/login/signin')
//   }

//   // 登录判断
//   const userStore = useUserStore()
//   if (!userStore.token && to.path !== '/login/signin') {
//     return next('/login/signin')
//   }

//   next()
// })

export default router
